<!--
 * @Descripttion:
 * @version:
 * @Author: 麦冬果果
 * @Date: 2021-07-08 14:54:38
 * @LastEditors: 麦冬果果
 * @LastEditTime: 2021-09-08 14:23:36
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="接口状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择接口状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="应用名称" prop="aname">
        <el-input
          v-model="queryParams.aname"
          placeholder="请输入应用名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="应用组名称" prop="gname">
        <el-input
          v-model="queryParams.gname"
          placeholder="请输入应用组名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="cname">
        <el-input
          v-model="queryParams.cname"
          placeholder="请输入中文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="英文名称" prop="ename">
        <el-input
          v-model="queryParams.ename"
          placeholder="请输入英文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调用方法" prop="method">
        <el-select
          v-model="queryParams.method"
          placeholder="请选择调用方法"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in methodOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="调用主表名" prop="table_name">
        <el-select
          v-model="queryParams.table_name"
          placeholder="请选择调用主表"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in tableOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="sysAppApiList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="中文名称" align="center" prop="cname" />
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="正常"
            inactive-value="关闭"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="应用名称" align="center" prop="aname" /> -->
      <el-table-column label="接口组名称" align="center" prop="gname" />
      <el-table-column label="英文名称" align="center" prop="ename" />
      <el-table-column label="调用方法" align="center" prop="method" />
      <el-table-column label="调用主表名" align="center" prop="table_name" />
      <el-table-column label="hook方式" align="center" prop="hook_way" />
      <el-table-column label="hook名称" align="center" prop="hook_name" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEditTable(scope.row)"
            >数据</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改应用接口对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="应用名称" prop="aname">
          <el-input v-model="form.aname" placeholder="请输入应用名称" />
        </el-form-item> -->
        <el-form-item label="接口组名称" prop="gname">
          <el-input v-model="form.gname" placeholder="请输入应用组名称" />
        </el-form-item>
        <el-form-item label="调用表" prop="table_name">
          <el-select
            v-model="form.table_name"
            placeholder="选择调用表"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in tableOption"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="接口状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictLabel"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="接口中文名称" prop="cname">
          <el-input v-model="form.cname" placeholder="请输入中文名称" />
        </el-form-item>

        <el-form-item label="接口英文名称" prop="ename">
          <el-input v-model="form.ename" placeholder="请输入英文名称" />
        </el-form-item>

        <el-form-item label="调用方法" prop="method">
          <el-tooltip content="留空则生成crud" placement="top">
            <i class="el-icon-question"></i>
          </el-tooltip>
          <el-select
            v-model="form.method"
            placeholder="选择调用方法"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in methodOption"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <!-- table_name -->

        <el-form-item label="hook方式" prop="hook_way">
          <el-select
            v-model="form.hook_way"
            placeholder="选择hook方式"
            clearable
            size="small"
            style="width: 240px"
            @change="getHookName"
          >
            <el-option
              v-for="dict in hookOption"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="hook名称" prop="hook_name">
          <el-select
            v-model="form.hook_name"
            placeholder="选择hook名称"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in hookNameOption"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  list,
  getOne,
  del,
  add,
  update,
  setStatus,
  getHookNameOption,
  getTableOption,
} from "@/api/fastApi";

export default {
  name: "sysAppApi",
  //components: { importTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用接口表格数据
      sysAppApiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // $comment字典
      statusOptions: [
        {
          dictValue: "正常",
          dictLabel: "正常",
        },
        {
          dictValue: "关闭",
          dictLabel: "关闭",
        },
      ],
      //tableList: [],
      // $comment字典

      methodOption: [
        {
          dictValue: "one",
          dictLabel: "one",
        },
        {
          dictValue: "all",
          dictLabel: "all",
        },
        {
          dictValue: "page",
          dictLabel: "page",
        },
        {
          dictValue: "add",
          dictLabel: "add",
        },
        {
          dictValue: "edit",
          dictLabel: "edit",
        },
        {
          dictValue: "delete",
          dictLabel: "delete",
        },
      ],
      tableOption: [],
      hookOption: [
        {
          dictValue: "BEFORE_SERVE",
          dictLabel: "BEFORE_SERVE",
        },
        {
          dictValue: "AFTER_SERVE",
          dictLabel: "AFTER_SERVE",
        },
        {
          dictValue: "BEFORE_OUTPUT",
          dictLabel: "BEFORE_OUTPUT",
        },
        {
          dictValue: "AFTER_OUTPUT",
          dictLabel: "AFTER_OUTPUT",
        },
      ],
      hookNameOption: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        //aname: null,
        gname: null,
        cname: null,
        ename: null,
        method: null,
        table_name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "接口状态不能为空", trigger: "blur" },
        ],
        // aname: [
        //   { required: true, message: "应用名称不能为空", trigger: "blur" }
        // ],
        gname: [
          { required: true, message: "应用组名称不能为空", trigger: "blur" },
        ],

        table_name: [
          { required: true, message: "调用主表名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    getTableOption().then((resp) => {
      this.tableOption = [];
      let result = resp.data;
      //this.tableList = result;
      result.forEach((item) => {
        let table = {
          dictValue: item.table_name,
          dictLabel: item.table_name,
        };
        this.tableOption.push(table);
      });
    });
  },
  methods: {
    /** 打开导入表弹窗 */
    // openImportTable() {
    //   this.$refs.import.show();
    //   console.log(this.tableList);
    // },
    getHookName(value) {
      if (value == "") {
        return;
      }
      getHookNameOption(value).then((resp) => {
        this.hookNameOption = [];
        let result = resp.data;
        result.forEach((item) => {
          let table = {
            dictValue: item,
            dictLabel: item,
          };
          this.hookNameOption.push(table);
        });
      });
    },
    /** 查询应用接口列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.sysAppApiList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // $comment字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    handleStatusChange(row) {
      console.log(row);
      let text = row.status === "正常" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.cname + '"应用吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return setStatus(row.id, row.status);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "正常" ? "关闭" : "正常";
        });
    },
    // $comment字典翻译
    // methodFormat(row, column) {
    //   return this.selectDictLabel(this.methodOptions, row.method);
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        app_id: null,
        status: "0",
        //aname: null,
        gname: null,
        cname: null,
        ename: null,
        method: null,
        table_name: null,
        table_join: null,
        where_by: null,
        order_by: null,
        group_by: null,
        field_by: null,
        created_at: null,
        updated_at: null,
        created_by: null,
        updated_by: null,
      };
      this.resetForm("form");
    },
    /** 数据按钮操作 */
    handleEditTable(row) {
      // this.$router.push("/apiTable/data");
      this.$router.push({
        path: "/apiData",
        query: { id: row.id },
        //params: row
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应用接口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOne(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应用接口";
        if (response.data.hook_way) {
          this.getHookName(response.data.hook_way);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            update(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            add(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除应用接口编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return del(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  }, //methods结束
};
</script>
